<?
require_once("conn.php");
?>
<script>
	function updateRemitem(){
		var selCat = $("#selCat").val();
		if (selCat != "")
		{
			$.ajax({
			   type: "POST",
			   url: "process.php?cmd=showRemoveStock",
			   data: "&id="+selCat+"&t="+Math.random(),
			   success: function(msg){
				   $("#showItem").html(msg);
			   }
			});
		}
	}

	function updateRemOffice(){
		var tmp = $("#rem_item").val().split(":");
		$("#rem_id").val(tmp[0]);
		$("#rem_uid").val(tmp[1]);
		$("#showUnit").html(tmp[2]);
		$("#itemname").val(tmp[3]);
		$("#expired").val(tmp[4]);
		$("#barcode").val(tmp[5]);
		//alert($("#rem_item").val());
		//alert(tmp[0]+":"+tmp[1]+":"+tmp[2]);


	}

	function addrowRemItem(){
		var rem_id = $("#rem_id").val();
		var rem_uid = $("#rem_uid").val();
		var rem_uname = $("#showUnit").html();
		var rem_amount = $("#rem_amount").val();
		var rem_name = $("#itemname").val();
		var rem_barcode = $("#barcode").val();
		var rem_expired = $("#expired").val();

		//alert(rem_id+":"+rem_uid+":"+rem_uname+":"+rem_amount+":"+rem_name+":"+rem_barcode+":"+rem_expired);
		
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=remStockOfficeCart",
		   data: "&rem_id="+rem_id+"&rem_uid="+rem_uid+"&rem_amount="+rem_amount+"&rem_name="+rem_name+"&rem_barcode="+rem_barcode+"&rem_expired="+rem_expired+"&rem_uname="+rem_uname+"&t="+Math.random(),
		   success: function(msg){
				if(msg.match("ok:")){
					updateRemStockCart();
				}else{
					alert(msg);
				}
			}
		});		
	}

	function updateRemStockCart(){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=updateRemStockCart",
		   data: "t="+Math.random(),
		   success: function(msg){
			   //alert(msg);
				$('#remStockList').html(msg);
				//alert($("#or_list").html());
		   }
		});
	}

	function delRemStockOfficeItem(id){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=remFromRemStockOfficeList",
		   data: "&id="+id+"&t="+Math.random(),
		   success: function(msg){
			   updateRemStockCart();
		   }
		});
	}

	function saveStockRemove(){

		if(confirm('Do you want to Save ?')==true){
			var item = new Array();
			var amount = new Array();
			var unit = new Array();
			var barcode = new Array();
			var exp = new Array();

			

			$("input[name^=send_i]").each(function(i) {
				//alert(i+":"+$(this).val());
				item[i] = $(this).val();
			});

			$("input[name^=send_a]").each(function(i) {
				//alert(i+":"+$(this).val());
				amount[i] = $(this).val();
			});

			$("input[name^=send_u]").each(function(i) {
				//alert(i+":"+$(this).val());
				unit[i] = $(this).val();
			});

			$("input[name^=send_b]").each(function(i) {
				//alert(i+":"+$(this).val());
				barcode[i] = $(this).val();
			});

			$("input[name^=send_exp]").each(function(i) {
				//alert(i+":"+$(this).val());
				exp[i] = $(this).val();
			});


			//alert(item.length);

			$.ajax({
			   type: "POST",
			   url: "process.php?cmd=remStockOffice",
			   data: { "item[]" : item , "amount[]" : amount , "unit[]" : unit , "barcode[]" : barcode , "exp[]" : exp , "t" : Math.random() },
			   success: function(msg){
				   if(msg.match("ok:")){
						alert("Success");
						OfficeStore();
						//var id = msg.split(":");
						//loadEffect('Download Send Order <a href="sendIslandExcel.php?id='+id[1]+'"><img src="images/excel_icon.jpg" width="20"></a>');
						//getExcelForm();
				   }else{
						alert(msg);
				   }
					//loadUnitManage();
			   }
			});
		}
	}
</script>
<table>
	<tr>
		<td>Category : </td>
		<td>
			<select id="selCat" onchange="updateRemitem()">
				<option value=""></option>
			<?
					$sql = "SELECT category_id,category_name FROM (pch_stock_office as a join pch_product as b on a.item_id=b.id) LEFT JOIN pch_category as c on b.category_id=c.id GROUP BY category_id;";
					$query = mysql_query($sql);
					while($row = mysql_fetch_array($query)){
					?>
						<option value="<?=$row["category_id"]?>"><?=$row["category_name"]?></option>
					<?
					}
			?>
			</select>
		</td>
	</tr>
	<tr>
		<td>Item : </td>
		<td><div id="showItem"><select><option value="">Select Category First</option></select></div></td>
	</tr>
	<tr>
		<td>Amount : </td>
		<td><input type="text" id="rem_amount">&nbsp;&nbsp;<span id="showUnit"></span></td>
	</tr>
	<tr>
		<td><input type="hidden" id="rem_id"><input type="hidden" id="rem_uid"><input type="hidden" id="itemname"><input type="hidden" id="expired"><input type="hidden" id="barcode"><input type="button" value="Add" onclick="addrowRemItem()"></td>
	</tr>
</table>
<script>updateRemStockCart();</script>
<div id="remStockList"></div>